package gwtI18nEditor.widgets;

import gwtI18nEditor.datamodel.Directory;
import gwtI18nEditor.datamodel.DirectoryEntry;
import gwtI18nEditor.datamodel.MessageFile;
import gwtI18nEditor.resources.ImageLibrary;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;

@SuppressWarnings("serial")
public abstract class DirectoryListing extends JScrollPane implements TreeSelectionListener, TreeCellRenderer {
	
	private static final DefaultTreeCellRenderer RENDERER = new DefaultTreeCellRenderer();
	
	private MessageFile            lastSelection;
	private DefaultTreeModel       treeModel;
	private JTree 				   tree;

	public DirectoryListing() {
		super();
		setMinimumSize(new Dimension(100, 200));
		treeModel = new DefaultTreeModel(new DefaultMutableTreeNode("loading...", true));
		tree = new JTree(treeModel);
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.addTreeSelectionListener(this);
		tree.setCellRenderer(this);
		setViewportView(tree);
	}
	
	public void updateTree(Directory root) {
		DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(root, true);
		parseDirectory(rootNode, root);
		treeModel.setRoot(rootNode);
		for (int i=0; i<tree.getRowCount(); i++)
			tree.expandRow(i);
	}
	
	private void parseDirectory(DefaultMutableTreeNode treeParent, Directory dirParent) {
		for (Directory dir: dirParent.getSubdirectories()) {
			if (!dir.isEmpty()) {
				DefaultMutableTreeNode node =
					new DefaultMutableTreeNode(dir, true);
				treeParent.add(node);
				parseDirectory(node, dir);
			}
		}
		for (MessageFile msg: dirParent.getFiles()) {
			DefaultMutableTreeNode node =
				new DefaultMutableTreeNode(msg, false);
			treeParent.add(node);
		}
	}

	@Override
	public void valueChanged(TreeSelectionEvent arg0) {
		DefaultMutableTreeNode node = 
			(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
		if (node==null) return;
		if ((node.getUserObject()==null) || !(node.getUserObject() instanceof MessageFile)) return;
		MessageFile message = (MessageFile) node.getUserObject();
		if (message==lastSelection) return;
		lastSelection = message;
		onSelect(message);
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,	boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
		JLabel result = (JLabel) RENDERER.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
		if ((value==null) || !(value instanceof DefaultMutableTreeNode)) return result;
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
		if (node.getUserObject()==null) return result;
		if (node.getUserObject() instanceof DirectoryEntry) {
			DirectoryEntry file = (DirectoryEntry) node.getUserObject();
			OverlayIcon icon =
				new OverlayIcon(ImageLibrary.get(file.isDirectory() ? "folder.png" : "message_file.png"));
			if (file.needsAttention()) icon.setOverlay(ImageLibrary.get("attention.png"));
			result.setIcon(icon);
		}
		return result;
	}
	
	public abstract void onSelect(MessageFile message);
}
